#!/bin/sh
#
# @(#)nsrdmo.sh 1.7 96/12/20 Copyright (c) 1996, Legato Systems, Inc.
#
# All rights reserved.
#
# nsrdmo
#
# This script is part of the Database Module for Oracle.
# Modification of this script should be done with care and only after reading
# the administration manual included with this product.
#
# This script should only be run as part of a scheduled savegroup.
#
# Returns 0 on success; 1 on failure.
#

#
# Configuration variables, modifiable by users.
#

#
# Variable: OBK_HOME
# Default value: site specific
# Description: Specifies where the Enterprise Backup Utility is installed.
# This variable must be configured in order to locate the obackup executable
#
OBK_HOME=

#
# Variables: BRD_TOT_TIME, BRD_SAMP_TIME, BRD_ERR_TIME, BRD_RETRIES 
# Description: The noted environment variables allow the user to 
# configure the EBU Instance Manager (brd).  The default settings for these
# variables as specified by Oracle are not compatible with scheduled saves
# from NetWorker where obackup starts a new brd process.  If brd is started
# using the default values for these variables then savegrp will appear to
# appear to hang before backing up the indexes.  The following settings
# will prevent the described behaviour by causing brd to exit seconds after
# the backup is completed.  Consult the Oracle7 Enterprise Backup Utility 
# Administrator's Guide Release 2.0 page 6-4 for more information about
# these variables.
#

#
# Variable: BRD_TOT_TIME
# Default value: 300
# Oracle Default value: 172800
# Description: Once the Instance Manager (brd) becomes idle, it will wait for
# BRD_TOT_TIME seconds for any activity by obackup before it exits.
# Caution: setting this value too low may cause brd to exit before obackup
# can establish a connection to the Backup Catalog. If this happens obackup
# will fail with the following error:
# OBK-2464: Internal error.  Instance Manager process was not started
#
BRD_TOT_TIME=300

#
# Variable: BRD_SAMP_TIME
# Default value: 1
# Oracle Default value: 300
# Description: The Instance Manager (brd) checks the Backup Catalog for any
# active jobs in the interval of BRD_SAMP_TIME seconds.
#
BRD_SAMP_TIME=1

#
# Variable: BRD_ERR_TIME
# Default value: 1
# Oracle Default value: 900
# Description: If the Instance Manager (brd) detects an error it waits
# BRD_ERR_TIME before retrying the catalog.
#
BRD_ERR_TIME=1

#
# Variable: BRD_RETRIES
# Default value: 1
# Oracle Default value: 3
# Description: The Instance Manager (brd) looks for active jobs BRD_RETRIES
# number of times before going to sleep.
#
BRD_RETRIES=1

# Variable: PATH
# Default value: platform dependent
# Set up PATH environment variable.  This must be configured to include
# path to the NetWorker program "mminfo" and /usr/bin.
# This variable must be configured to locate the correct executables
#
# HP-UX 10.x
#PATH=/opt/networker/bin:/usr/bin
# HP-UX 9.x
#PATH=/usr/networker/bin:/usr/bin
# Solaris
#PATH=/bin:/usr/sbin:/usr/bin
# AIX
#PATH=/usr/bin
# All Platforms
PATH=/bin:/usr/sbin:/usr/bin:/opt/networker/bin:/usr/networker/bin

# For HP-UX only
# Variable: SHLIB_PATH
# Default value: /usr/lib
# obackup requires this variable to be set in order to locate the libobk.sl
# shared library.
# This variable should be configured on HP-UX systems.
#
SHLIB_PATH=/usr/lib

#
# Variable: PRECMD
# Default value: NONE
# Description: This variable can be used to run a command before oracle
# backups.  
# The pre command must return a non zero exit code if it fails.  It is
# the resposibility of the script writer to ensure that the script 
# returns the proper exit code.  Please refer chapter 5 of the BusinesSuite
# Module for Oracle Administrator's Guide UNIX Version for templates and
# examples.
# Setting this variable is optional.
#
PRECMD=

#
# Variable: POSTCMD
# Default value: NONE
# Description: This variable can be used to run a command after oracle
# backups have completed. 
# The post command must return a non zero exit code if it fails.  It is
# the resposibility of the script writer to ensure that the script 
# returns the proper exit code.  Please refer chapter 5 of the BusinesSuite
# Module for Oracle Administrator's Guide UNIX Version for templates and
# examples.
# Setting this variable is optional.
#
POSTCMD=

#
# Variable: NSR_DATA_VOLUME_POOL
# Default value: none
# Description: If this variable is set to name a media pool, then database
# backups will be directed to the named media pool.
# If not set DMO will use the "Default" pool.
# Setting this variable is optional.
#
NSR_DATA_VOLUME_POOL=


#
# Variable: NSR_NO_BUSY_ERRORS
# Default value: FALSE
# Description: If this variable is set to a value of TRUE then DMO will
# will fail immediately if it encounters a busy server while initializing
# the session.
# If this variable is not set DMO will fail immediately
# Setting this variable is optional.
#
NSR_NO_BUSY_ERRORS=FALSE


#
# Variable: NSR_CHECKSUM
# Default value: TRUE
# Description: This environment variable specifies checksumming
# is performed on the data.
# If this variable is not set DMO will use checksumming by default.
# Setting this variable is optional.
#
NSR_CHECKSUM=TRUE

#
# Variable: NSR_ENCRYPTION
# Default value: FALSE
# Description: This environment variable specifies encryption
# is performed on the data.
# If this variable is not set DMO will not use encryption by default.
# Setting this variable is optional.
#
NSR_ENCRYPTION=FALSE

#
# Variable: NSR_COMPRESSION
# Default value: FALSE
# Description: This environment variable specifies compression
# is performed on the data.
# If this variable is not set DMO will not use compression by default.
# Setting this variable is optional.
#
NSR_COMPRESSION=FALSE

#
# Variable: NSR_DEBUG_FILE
# Default value: none
# Description: To enable debugging output for DMO set the following to 
# an appropriate path and file name.  
# Setting this variable is for debugging purposes only 
#
#NSR_DEBUG_FILE=

#
# Not Supported
# Variable: OBK_DEVDBG
# Default value: none
# Description: Set as instructed by Technical Support.
# Setting this variable is for debugging purposes only 
#OBK_DEVDBG=0x00000000

#
# Shell and environment variables not configured by user.
#
NSR_SERVER=""		# Server to backup to (passed by savegrp)
echo=echo		# echo command
Verbose=0		# Are we generating verbose output?
Pid=0			# Which process to kill when we are cancelled

#
# usage(): display correct usage for nsrdmo
#
usage()
{
	${echo} \
"This command should only be executed by savegroup."
	${echo} \
"usage: $0 [<options>]
    Options:
	[ -BEiLnqvx ] [ -s server ] [ -N name ] [ -e expiration ]
	[  -f dirfile ] [ -b pool ] [ -F file ] [ -g group ]
	[ -l level ] [ -t date ] [ -m masquerade ] [ -W width ] [ path ... ]

For an explanation of all options, see the save(8) man page.
"
	exit 1
}


#
# Run the pre-processing command.  This can be used to shut down
# a database.
#
run_precmd()
{
	if [ "${PRECMD}" = "" ]; then
		return 0
	fi

	vecho
	vecho "Executing pre-processing command '${PRECMD}' at `date`"
	vecho

	${PRECMD}
	return_status=$?

	vecho
	vecho "Pre-processing command '${PRECMD}' finished at `date`"
	vecho

	return $return_status
}

#
# Run the post-processing command.  This can be used to start a database.
#
#
run_postcmd()
{
	if [ "${POSTCMD}" = "" ]; then
		return 0
	fi

	vecho
	vecho "Executing post-processing command '${POSTCMD}' at `date`"
	vecho

	${POSTCMD}
	return_status=$?

	vecho
	vecho "Post-processing command '${POSTCMD}' finished at `date`"
	vecho

	return $return_status
}

#
# Process arguments supplied by savegrp.  Must handle any arguments save(8)
# accepts.
#
# Sample list of arguments
# save -s empire -g Default -n -LL -m empire -l full -W78 -N /usr /usr
#
processargs()
{
while [ $# -gt 0 ]; do
	case "$1" in
	-s )	# Specify server
		NSR_SERVER=$2
		shift
		shift
		;;
	-B )	# Force connecting directory information
		Bflag=y
		shift
		;;
	-E )	# Estimate save set size
		Eflag=y
		shift
		;;
	-i )	# Ignore any directives
		iflag=y
		shift
		;;
	-LL )	# Force local backup and cause extra line to be printed
		LLflag=y
		shift
		;;
	-n)	# No save
		nflag=y
		shift
		;;
	-q )	# Quiet
		qflag=y
		shift
		;;
	-v )	# Verbose
		Verbose=1
		shift
		;;
	-N )	# Save set name 
		Saveset_name=$2
		shift
		shift
		;;
	-e )	# Specify alternate expiration time
		NSR_SAVESET_EXPIRATION=$2
		shift
		shift
		;;
	-f )	# Specify directives
		Directives=$2
		shift
		shift
		;;
	-b )	# Specify pool
		NSR_DATA_VOLUME_POOL=$2
		shift
		shift
		;;
	-F )	# Specify file for modification time comparison (not used)
		Directives=$2
		shift
		shift
		;;
	-g )	# Specify group
		Group=$2
		shift
		shift
		;;
	-l )	# Specify level
		Level=$2
		shift
		shift
		;;
	-t )	# Specify time for comparison
		Comparison_time=$2
		shift
		shift
		;;
	-m )	# Masquerade as another program
		Masquerade=$2
		shift
		shift
		;;
	-W )	# Specify width of output
		Output_width=$2
		shift
		shift
		;;
	*)	# Name of the obackup script
		obk_commands=$1
		shift
		;;
	esac
done
}

#
# Make sure all environment variables are exported.
#
export_environment_variables()
{
	export NSR_SERVER
	export NSR_COMPRESSION
	export NSR_ENCRYPTION
	export NSR_CHECKSUM
	export NSR_SAVESET_EXPIRATION
	export OBK_HOME
	export PATH
#	export NSR_DATA_VOLUME_POOL

# The following variables control EBU Instance Manager operation
	export BRD_TOT_TIME
	export BRD_SAMP_TIME
	export BRD_ERR_TIME
	export BRD_RETRIES

# The following variable is required on HP-UX
	export SHLIB_PATH

# The following variables are for debugging only.
#	export NSR_DEBUG_FILE 
#	export OBK_DEVDBG
}

#
# backup_oracle: run obackup to backup the oracle database
# The particulars of the backup are contained in the obackup 
# command script which is passed to obackup
# nsrobkstart must be on the PATH.
#
backup_oracle()
{

#	$OBK_HOME/bin/obackup $obk_commands
	nsrobkstart $obk_commands &
 	Pid=$!
 	wait $Pid
	obackup_status=$?

	if [ $obackup_status != 0 ]; then
		echo "obackup returned status of "$obackup_status
	fi
}

#
# Echo the arguments only if in verbose mode.
#
vecho()
{
	if [ $Verbose -gt 0 ]; then
		echo $*
	fi
}

#
# Display output exactly like save does to keep savegroup happy. 
# displays size information for database backups.  
#
display_summary()
{
	Hostname=`uname -n`
        /usr/bin/echo "$Hostname: Oracle Backup Status             level=full,        \c"
       mminfo -s $NSR_SERVER -q"savetime>=$Start_time,savetime<=$End_time" -r "name,totalsize(1),nfiles(1)" | \
awk '\
BEGIN { total_kb = 0; total_mb = 0; total_files = 0; files_str = "file" } \
{ \
        if ( $3 == "KB" ) { \
               total_kb += $2 \
       } else { \
               total_mb += $2 \
       } \
       total_files += $4 \
} \
END { \
       if (total_files > 1) {  \
               files_str = "files"  \
       } else {  \
               files_str = "file" \
       } \
        if (total_kb > 1000) { \
               total_mb += (total_kb / 1000) \
       } \
        if (total_mb > 0) {  \
printf "%d.%d MB 00:00:00  %d %s\n", total_mb, total_kb % 1000, total_files, files_str \
       } else {  \
printf "%d KB 00:00:00  %d %s\n", total_kb, total_files, files_str  \
       } \
}'

	return 0

}

#
# Handle cancel signals sent by savegrp when user stops the group.
# obackup likes to be cancelled with a SIGHUP
#
handle_signal()
{
	if [ $Pid != 0 ]; then
		kill -1 $Pid
	fi
	exit 1
}

#
# The main portion of this shell.
#
# Process arguments, export environment
# Execute a pre-command (if any)
# Execute obackup backup
# Execute a post-command
# Display save output

processargs $*

#
# Make sure we respond to savegrp cancellations.
#
trap handle_signal 2 15

#
# Export all environment variables.
#
export_environment_variables

#
# Handle pre-processing
#
run_precmd
precmd_status=$?
if [ $precmd_status != 0 ]; then
	echo "Pre-processing command failed.  Ending session."
	exit 1
else
	vecho "Pre-processing command succeeded."
fi

#
# Handle database backup
#
Start_time=`date`
backup_oracle
End_time=`date`

#
# handle post-processing
#
run_postcmd
postcmd_status=$?
if [ $postcmd_status != 0 ]; then
	echo "Post-processing command failed.  Ending session."
	if [ $obackup_status != 0 ]; then
		exit $obackup_status
	else
		exit 1
	fi
else
	vecho "Post-processing command succeeded."
	if [ $obackup_status != 0 ]; then
		exit $obackup_status
	fi
fi

#
# Display summary for savegroup
# Only displays if pre-command, obackup, and post-command were all
# successful.
#
display_summary

exit 0
